/// <reference path="../_all.ts" />

module Application.Controllers {

    export class InteractiveController implements Application.Interfaces.Controllers.IController {
        public static $inject = [
            '$rootScope',
            '$scope',
            '$location',
            '$routeParams',
            'PlayerService',
            'InteractiveService',
            'TranslationService',
        ];

        bansResult: {
            (data): void;
            (data, err): void;
        };

        constructor(public RootScope: Application.Interfaces.Scopes.IRootScope,
            public Scope: Application.Interfaces.Scopes.IInteractiveScope,
            public Location: ng.ILocationService,
            public RouteParams: Application.Interfaces.Routing.IServerRouteParams,
            public Players: Application.Interfaces.Services.IPlayerService,
            public Interactive: Application.Interfaces.Services.IInteractiveService,
            public Translations: Application.Interfaces.Services.ITranslationService) {

                this.Scope.vm = this;
                this.Scope.user = this.RootScope.user;
                this.Scope.location = this.Location;
                this.Scope.messages = new Application.Utility.Messages();
                this.Scope.navigations = new Application.Utility.Navigations();

                this.Scope.navigations.AddLink(Translations.Translate("SERVERLIST"), "/servers", false, false, Application.Constants.Icons.Servers);
                this.Scope.navigations.AddLink(Translations.Translate("ACTIONS"), "/actions", false, false, Application.Constants.Icons.Actions);
                this.Scope.navigations.AddLink(Translations.Translate("PLAYERS"), "/players", true, false, Application.Constants.Icons.Bans);

                if (this.Scope.user && this.Scope.user.role && this.Scope.user.role > 4) {
                    this.Scope.navigations.AddLink(Translations.Translate("USERS"), "/users", false, false, Application.Constants.Icons.Users);
                };
                this.Scope.navigations.AddLinkRight(Translations.Translate("ACCOUNT"), "/users/" + this.Scope.user.user_id, false);
                this.Scope.navigations.AddLinkRight(Translations.Translate("LOGOUT"), "/", false);

                this.bansResult = (data, err?) => {
                    this.bansResultProxy.apply(this, [data, err]);
                };
        }

        public log(msg: string) {
            //console.log(msg);
        }

        public banPlayers() {
            this.log('banPlayers');
            //this.Players.BanPlayers(players, this.bansResult);
        }

        private bansResultProxy(data, err) {
            this.log('bans result');
            if (data != null) {
                if (data instanceof Array) {
                    //var players = <Application.Resources.IPlayer[]> data;
                    //if (players != null && players.length > 0) {
                    //    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.success, "Info", new Application.Utility.String(this.Translations.Translate('PLAYERSUPDATED')).format(players.length.toString()));
                    //    var result: Application.Resources.IPlayer;
                    //    for (var i = 0; i < this.Scope.players.length; i++) {
                    //        result = null;
                    //        for (var j = 0; j < players.length; j++) {
                    //            if (this.Scope.players[i].player_id == players[j].player_id) {
                    //                result = players[j];
                    //                this.ensurePlayerBan(result);
                    //                players.splice(j, 1);
                    //                break;
                    //            };
                    //        };
                    //        if (result != null) {
                    //            this.Scope.players[i] = result;
                    //        };
                    //        if (players.length <= 0) {
                    //            break;
                    //        };
                    //    };
                    //    for (var i = 0; i < players.length; i++) {
                    //        result = players[i];
                    //        this.ensurePlayerBan(result);
                    //        this.Scope.players.push(result);
                    //    };
                    //    this.Scope.playersListForm.$setPristine();
                    //} else {
                    //    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('NOPLAYERSFOUND'));
                    //};
                } else {
                    //var player = <Application.Resources.IPlayer> data;
                    //if (player != null) {
                    //    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.success, "Info", new Application.Utility.String(this.Translations.Translate('PLAYERUPDATED')).format(player.player_name));
                    //    for (var i = 0; i < this.Scope.players.length; i++) {
                    //        if (this.Scope.players[i].player_id == player.player_id) {
                    //            this.ensurePlayerBan(player);
                    //            this.Scope.players[i] = player;
                    //            break;
                    //        };
                    //    };
                    //} else {
                    //    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('NOPLAYERUPDATED'));
                    //};
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', this.Translations.Translate('UNKNOWNERROR'));
                };
            };
        }

    };
}